//
//  BaseSearchChooseViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/23.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "BaseSearchChooseViewController.h"
#import "ChooseTableViewCell.h"
#import "ChooseListModel.h"
#import "ChooseView.h"
#import "DatePickView.h"
#import "TB_FMDBManager.h"
#import "HistoryModel.h"
#import "HistoryShowTableViewCell.h"
#import "RSTableHeadView.h"
#import "BaseProvideView.h"
#import "DatePickView.h"
#import "ChooseMoreObjcViewController.h"
#import "RegisterSingleTableViewCell.h"
#import "PositionSelectView.h"
#import "SearchBaseView.h"
@interface BaseSearchChooseViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView *homeTableView;

@property(nonatomic,strong)ChooseListModel *Cmodel;

@property(nonatomic,copy)NSString*SearchWord;

@property(nonatomic,strong)NSMutableArray *HistoryArr;

@property(nonatomic,strong)NSMutableArray *CoventArr;

@property(nonatomic,strong)HistoryModel *InsertModel;

@property(nonatomic,strong)NSMutableDictionary *BlockDict;//总筛选参数

@property(nonatomic,strong)NSMutableArray *SavelistArr;//筛选列表存储本地，第二次进来不请求接口

@property(nonatomic,strong)NSMutableArray *SaveReqArr;//储存本地请求的数据,第二次进来不需要请求接口

@property(nonatomic,strong)SearchBaseView *Searchview;

@property(nonatomic,assign)BOOL isMinFreshRtext;
@property(nonatomic,assign)BOOL isMaxFreshRtext;
@end

@implementation BaseSearchChooseViewController



-(void)viewWillAppear:(BOOL)animated
{
    
    if (!self.isHiddenSearch) {
        
        self.StatusBarStyle=UIStatusBarStyleDefault;
        
        [self.navigationController.navigationBar addSubview:self.Searchview];
        
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
        
    }
    
    
}

-(void)viewWillDisappear:(BOOL)animated
{
     if (!self.isHiddenSearch) {
     self.StatusBarStyle=UIStatusBarStyleLightContent;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kColorRGBA(0x2E4361, 1)] forBarMetrics:UIBarMetricsDefault];
    
    [self.Searchview removeFromSuperview];
         
     }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

     if (!self.isHiddenSearch) {
    
         self.isShowLiftBack=NO;
     }else{
         
         self.title=@"筛选";
     }
    //如果存在请求数据的，加载数据
    if (self.ReqStr.length>0) {
        
         [self loadDatas];
        
    }else{
        
        if (self.paradic!=nil||[self.paradic count]!=0) {
            
            //代表存在接口请求的筛选，二次进来，不用请求接口，读取本地的
            NSArray *arr=[USER_DEFAULT objectForKey:@"TBReqChoose"];
            
            for (NSDictionary *dict in arr) {
       
                [self.SaveReqArr addObject:[ChooseListData mj_objectWithKeyValues:dict]];
                
            }
            
             [self.homeTableView reloadData];
        }
   
    }
    
     if (!self.isHiddenSearch) {
    //查询数据库历史记录
         [self QueryDB];
    
     }
    //处理数据使之为模型
    
    [self dealCzustomArr];
    
    [self creatProvideView];//提交按钮
    
    [self.view addSubview:self.homeTableView];
    
    // Do any additional setup after loading the view.
}

-(NSMutableArray*)CoventArr
{
    if (!_CoventArr) {
        
        _CoventArr=[NSMutableArray array];
        
    }
    
    return _CoventArr;
}

#pragma mark-处理数据
-(void)dealCzustomArr
{
    
    for (NSDictionary *dict in self.CustomArr) {
  
  
        [self.CoventArr addObject:[ChooseListData mj_objectWithKeyValues:dict]];
        
    }
    
     [self.homeTableView reloadData];

}

-(void)QueryDB
{
    [self.HistoryArr removeAllObjects];
    
   NSArray *arr= [[TB_FMDBManager shareDataBase]QueryData];
    
    [self.HistoryArr addObjectsFromArray:arr];
    
    [self.homeTableView reloadData];
}


-(SearchBaseView*)Searchview
{
    if (!_Searchview) {
        TBWeak;
        _Searchview=[[SearchBaseView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44) andBlock:^(NSString *arm, BOOL isPop) {
           
            if (isPop) {
             
                [weakSelf.navigationController popViewControllerAnimated:YES];
                
            }else{
            
                weakSelf.InsertModel.searchkey=arm;
                
                weakSelf.KeyWord=arm;
            
                weakSelf.InsertModel.isExist=NO;
            }
        }];

        _Searchview.SearchTF.text=weakSelf.KeyWord;
        
    }
    
    return _Searchview;
}

//反向传值
-(NSMutableDictionary*)BlockDict
{
    if (!_BlockDict) {
        
        _BlockDict=[NSMutableDictionary dictionary];
        
    }
    
    return _BlockDict;
}

-(NSMutableArray*)SavelistArr
{
    if (!_SavelistArr) {
        
        _SavelistArr=[NSMutableArray array];
        
    }
    
    return _SavelistArr;
}

-(NSMutableArray*)SaveReqArr
{
    if (!_SaveReqArr) {
        
        _SaveReqArr=[NSMutableArray array];
        
    }
    
    return _SaveReqArr;
}


-(HistoryModel*)InsertModel
{
    if (!_InsertModel) {
        
        _InsertModel=[[HistoryModel alloc]init];
        
    }
    
    return _InsertModel;
}

-(NSMutableArray*)HistoryArr
{
    if (!_HistoryArr) {
        
        _HistoryArr=[NSMutableArray array];
    }
    
    return _HistoryArr;
}


-(void)creatProvideView
{
    
    CGRect frame;
    
    if (kDevice_Is_iPhoneX) {
        
          frame=CGRectMake(0, SCREEN_HEIGHT-44*SCREEN_WIDTH/320.0-64-IPhoneXTableHeight, SCREEN_WIDTH, 44*SCREEN_WIDTH/320.0);
    }else{
        
        frame=CGRectMake(0, SCREEN_HEIGHT-44*SCREEN_WIDTH/320.0-64, SCREEN_WIDTH, 44*SCREEN_WIDTH/320.0);
        
    }
    
    BaseProvideView *provide=[[BaseProvideView alloc]initWithFrame:frame];
    
    [provide aspect_hookSelector:@selector(ResetAction) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspects){
        
        for (ChooseListData *data in self.CoventArr) {
            data.value=@"";
            data.items=@[];
            
            if ([data.fieldName isEqualToString:@"areaId1"]||[data.fieldName isEqualToString:@"areaId2"]||[data.fieldName isEqualToString:@"areaId3"]||[data.fieldName isEqualToString:@"areaId4"]||[data.fieldName isEqualToString:@"type"]||[data.fieldName isEqualToString:@"subjectId"]) {
                
                data.name=@"";
                
            }else if ([data.fieldName isEqualToString:@"minScore"] || [data.fieldName isEqualToString:@"maxScore"])
            {
                data.value = nil;
            }else if ([data.fieldName isEqualToString:@"catId"])
            {
                data.name=@"";
            }else if ([data.productType isEqualToString:@"商品分类"])//商品分类
            {
                data.name=@"";
            }
        }
        for (ChooseListData *reqData in self.SaveReqArr) {
            
            reqData.value=@"";
        }
        
        if (!self.isHiddenSearch) {
            
              _Searchview.SearchTF.text=@"";
        }

        self.InsertModel.searchdate=@"";
        
        [self.homeTableView reloadData];
        
    } error:nil];
    
    [provide aspect_hookSelector:@selector(FinshAction) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspects){
        
    if (self.Searchview.SearchTF.text.length!=0) {
            //插入历史记录
        
        if (self.InsertModel.isExist==NO) {
            
            self.InsertModel.searchdate=[HelpWay dateToStringWith:[NSDate date]];
            
            [[TB_FMDBManager shareDataBase]inSertHistoryWithModel:self.InsertModel];
            
        }
  
        [self.BlockDict setValue:self.Searchview.SearchTF.text forKey:@"keyword"];
   
        }

        NSMutableArray *UserCustomArr=[NSMutableArray array];
        
        for (ChooseListData *data in self.CoventArr) {
           
            [UserCustomArr addObject:data.mj_keyValues];
            
            //注意列表的:
            
            if ([data.fieldName isEqualToString:@"shopIds"]||[data.fieldName isEqualToString:@"marketIds"]||[data.fieldName isEqualToString:@"checkUserIds"]||[data.fieldName isEqualToString:@"subjectIds"]||[data.fieldName isEqualToString:@"supplierIds"]||[data.fieldName isEqualToString:@"markStaffIds"])
            {
                if (data.items.count!=0) {
                    
                    NSMutableArray *paraArr=[NSMutableArray array];
                    
                    for (ChooseListItems *sub in data.items) {
                        
                        [paraArr addObject:sub.value];
                        
                    }
                    
                     [self.BlockDict setValue:paraArr forKey:data.fieldName];
                    
                }
         
                
            }else{
    
            [self.BlockDict setValue:data.value forKey:data.fieldName];
     
            }
        }
        
        NSMutableArray *UseReqArr=[NSMutableArray array];
        
        for (ChooseListData *reqData in self.SaveReqArr) {
            
            [UseReqArr addObject:reqData.mj_keyValues];
    
          [self.BlockDict setValue:reqData.value forKey:reqData.fieldName];

        }
        
        NSString*Para= [HelpWay dictionaryToJSON:self.BlockDict];
   
        _SureBlock(Para,self.Searchview.SearchTF.text);
        
        //需要缓存 1.缓存自己的数据  2.缓存请求的数据
        
        [USER_DEFAULT setObject:UserCustomArr forKey:@"TBChoose"];
        
        [USER_DEFAULT setObject:UseReqArr forKey:@"TBReqChoose"];
        
        [USER_DEFAULT synchronize];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } error:nil];
    
    [self.view addSubview:provide];
    
}


-(void)loadDatas
{
    [self showLoadingAnimation];
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:self.ReqStr andrequestTime:5 andParams:self.paradic andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        [self stopLoadingAnimation];
        
        self.Cmodel=[ChooseListModel mj_objectWithKeyValues:responseObject];
        if (responseStatus==1) {
            //请求成功
   
            [self.SaveReqArr addObjectsFromArray:self.Cmodel.data];
            
        [self.homeTableView reloadData];
      
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
            self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
            }];
        }
  
    }];
    
    
}


-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        CGRect frame;
        
         if (kDevice_Is_iPhoneX) {
             
             frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-44*SCREEN_WIDTH/320.0-IPhoneXTableHeight);
             
         }else{
             
             frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-44*SCREEN_WIDTH/320.0);
             
         }
   
        _homeTableView=[[TB_TableView alloc]initWithFrame:frame style:UITableViewStylePlain];
        
        _homeTableView.estimatedRowHeight=44;
        
        _homeTableView.delegate=self;
        
        _homeTableView.dataSource=self;
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"ChooseTableViewCell" bundle:nil] forCellReuseIdentifier:@"Chcell"];
        
        [_homeTableView registerNib:[UINib nibWithNibName:@"RegisterSingleTableViewCell" bundle:nil] forCellReuseIdentifier:@"TypeCell1"];
        
        [_homeTableView registerClass:[HistoryShowTableViewCell class] forCellReuseIdentifier:@"HSYcell"];
        
        [_homeTableView registerClass:[RSTableHeadView class] forHeaderFooterViewReuseIdentifier:@"Head"];
        
        
        [self configureNeedShowEmptyDataSetScrollView:self.homeTableView];
        
        _homeTableView.tableFooterView=[[UIView alloc]init];
        
    }
    
    return _homeTableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 
    if (section==0) {
        
        if (self.HistoryArr.count==0) {
            
            return 0;
        }else{
        
        return 1;
            
        }
    }
    
    return self.SaveReqArr.count+self.CoventArr.count;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0) {
        
        HistoryShowTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"HSYcell" forIndexPath:indexPath];
        
        cell.dataArr=self.HistoryArr;
        
        cell.block=^(NSString *keyWord)
        {
           _Searchview.SearchTF.text=keyWord;
   
            self.InsertModel.searchkey=keyWord;
            
            self.InsertModel.isExist=YES;
        };
        
        return cell;
        
    }else{
    
        if (indexPath.row<self.CoventArr.count) {
            
            ChooseListData *data=self.CoventArr[indexPath.row];
            
            if([data.fieldName isEqualToString:@"minScore"] || [data.fieldName isEqualToString:@"maxScore"])
            {
                RegisterSingleTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TypeCell1" forIndexPath:indexPath];
                cell.isInputScore = YES;
                cell.TitleName=data.name;
                cell.VaildType = @"num";
                
                if(data.value == nil)
                {
                    cell.RtextFile.text = data.value;
                    cell.RtextFile.placeholder = @"请填写内容";
                }
                
                [cell.RtextFile removeAllBlocksForControlEvents:UIControlEventEditingChanged];
                
                [cell.RtextFile addBlockForControlEvents:UIControlEventEditingChanged block:^(id  _Nonnull sender) {
                    
                    UITextField *textf=sender;
                    
                    data.value=textf.text;
                    
                }];
                
                return cell;
            }else{
                ChooseTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"Chcell" forIndexPath:indexPath];
                data.isCheckGroup = self.isCheckGroup;
                cell.Wmodel=data;
                
                return cell;
            }
            return nil;
        }else{

            ChooseListData *data=self.SaveReqArr[indexPath.row-self.CoventArr.count];
    
            if (data.value.length==0) {
        
                data.value=@"0";
            }
            ChooseTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"Chcell" forIndexPath:indexPath];
  
            cell.Cmodel=data;
    
            return cell;
        
        }
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0) {
    
        if (self.HistoryArr.count==0) {
            
            return 0;
        }
        
        return 50;
    }
    
    return UITableViewAutomaticDimension;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.isHiddenSearch) {
        
        if (section==0) {
            
            return 0;
        }
        
    }
    return 30;
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    RSTableHeadView *head=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Head"];
    head.block=^()
    {
        [self QueryDB];
        
    };
    
    if (section==0) {
        
        head.Rtitle=@"历史记录";
        
        head.isShowDele=YES;
        
    }else{
        
        head.Rtitle=@"筛选条件";
        
         head.isShowDele=NO;
        
    }
    
    return head;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
    
    }else{
    
    if (indexPath.row<self.CoventArr.count) {
        
   
        //根据不同的key值出现不同选择
         ChooseListData *list=self.CoventArr[indexPath.row];
        
        if ([list.fieldName isEqualToString:@"startDate"]||[list.fieldName isEqualToString:@"endDate"]) {
            //时间选择器
            
            DatePickView *pick=[[DatePickView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,kDevice_Is_iPhoneX?SCREEN_HEIGHT-64-IPhoneXTableHeight: SCREEN_HEIGHT-64) andDate:[NSDate date] andblock:^(NSDate *selDate) {
                
                if ([list.fieldName isEqualToString:@"startDate"]) {
                    
                    list.value=[HelpWay dateToStringWith:selDate];
                    
                }else{
                    
                     list.value=[HelpWay dateToStringWith:selDate];
                    
                }
                
                [self.homeTableView reloadData];
                
            }];
            
            [pick PopWithView:self.view];
            
        }else if ([list.fieldName isEqualToString:@"shopIds"])
        {
            //商户列表
            
            ChooseMoreObjcViewController *ChooseMore=[ChooseMoreObjcViewController new];
            
            ChooseMore.title=@"选择商户";
            
            ChooseMore.ListUrl=@"/index.php/FoodCheckApi/shopList.json";
            
            //如果开办方进来要传markid
            
            if ([self getUserType]==3) {
             
                ChooseMore.ListParams=@{@"marketId":[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]]};
                
            }else{
            
                ChooseMore.ListParams=@{};
                
            }
            ChooseMore.OrinArr=list.items;
            
            ChooseMore.block=^(NSArray *ArmArr, NSInteger count)
            {
                list.items=ArmArr;
                
                if (count==0) {
                    
                    list.value=@"";
                }else{
                
                list.value=[NSString stringWithFormat:@"已选%ld个商户",count];
                }
                 [self.homeTableView reloadData];
                
            };
            
            [self.navigationController pushViewController:ChooseMore animated:YES];
            
            
        }else if ([list.fieldName isEqualToString:@"marketIds"])
        {
            //开办方列表
            
            ChooseMoreObjcViewController *ChooseMore=[ChooseMoreObjcViewController new];
            
            ChooseMore.ListUrl=@"/index.php/FoodCheckApi/marketList.json";
            
            if ([self getUserType]==2) {
                
                ChooseMore.ListParams=self.paradic;
                
            }else{
            
                ChooseMore.ListParams=@{};
                
            }
             ChooseMore.title=@"选择开办方";
            
            ChooseMore.OrinArr=list.items;
            
            ChooseMore.block=^(NSArray *ArmArr, NSInteger count)
            {
                 list.items=ArmArr;
                
                if (count==0) {
                 
                    list.value=@"";
                }else{
                
                 list.value=[NSString stringWithFormat:@"已选%ld个开办方",count];
                }
                 [self.homeTableView reloadData];
            };
            
            [self.navigationController pushViewController:ChooseMore animated:YES];
            
        }else if ([list.fieldName isEqualToString:@"checkUserIds"])
        {
            //检查员列表
            
            ChooseMoreObjcViewController *ChooseMore=[ChooseMoreObjcViewController new];
            
            ChooseMore.ListUrl=@"/index.php/FoodCheckApi/checkerList.json";
            
            ChooseMore.ListParams=@{@"type":@"2"};
            
            ChooseMore.OrinArr=list.items;
            
             ChooseMore.title=@"选择检查员";
            
            ChooseMore.block=^(NSArray *ArmArr, NSInteger count)
            {
                 list.items=ArmArr;
                
                if (count==0) {
                    
                    list.value=@"";
                }else{
                
                 list.value=[NSString stringWithFormat:@"已选%ld个检查员",count];
                }
                 [self.homeTableView reloadData];
                
            };
            
            [self.navigationController pushViewController:ChooseMore animated:YES];
            
            
        }else if ([list.fieldName isEqualToString:@"markStaffIds"])  {
            //管理员列表
            
            ChooseMoreObjcViewController *ChooseMore=[ChooseMoreObjcViewController new];
            
            ChooseMore.ListUrl=@"/index.php/FoodCheckApi/checkerList.json";
            
            ChooseMore.ListParams=@{@"type":@"1"};
            
            ChooseMore.OrinArr=list.items;
            
            ChooseMore.title=@"选择考官";
            
            ChooseMore.block=^(NSArray *ArmArr, NSInteger count)
            {
                list.items=ArmArr;
                
                if (count==0) {
                    
                    list.value=@"";
                }else{
                    
                    list.value=[NSString stringWithFormat:@"已选%ld个考官",count];
                }
                [self.homeTableView reloadData];
                
            };
            
            [self.navigationController pushViewController:ChooseMore animated:YES];
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
        else if ([list.fieldName isEqualToString:@"supplierIds"]){
     
            ChooseMoreObjcViewController *ChooseMore=[ChooseMoreObjcViewController new];
            
            ChooseMore.ListUrl=@"/index.php/FoodCheckApi/supplierList.json";
            
            if ([self getUserType]==1) {
                
                ChooseMore.ListParams=@{@"shopId":[NSString stringWithFormat:@"%ld",[LoginStatuesModel shopId]]};
                
            }else{
            
                if (self.Lshopid!=0) {
                    
                     ChooseMore.ListParams=@{@"shopId":[NSString stringWithFormat:@"%ld",self.Lshopid]};
                    
                }else{
                
                    if ([self getUserType]==3) {
                     
                        ChooseMore.ListParams=@{@"marketId":[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]]};
                        
                    }else{
                    
                    ChooseMore.ListParams=@{};
            
                    }
                }
            }
            ChooseMore.title=@"选择供应商";
            
            ChooseMore.OrinArr=list.items;
            
            ChooseMore.block=^(NSArray *ArmArr, NSInteger count)
            {
                list.items=ArmArr;
                
                if (count==0) {
                    
                    list.value=@"";
                }else{
                    
                    list.value=[NSString stringWithFormat:@"已选%ld个供应商",count];
                }
                [self.homeTableView reloadData];
            };
            
            [self.navigationController pushViewController:ChooseMore animated:YES];
        
        }else if ([list.fieldName isEqualToString:@"subjectIds"]){
            
            //检查主题
            
            ChooseMoreObjcViewController *ChooseMore=[ChooseMoreObjcViewController new];
            
            ChooseMore.ListUrl=@"/index.php/FoodCheckApi/inspectSubjectList.json";
            
            if ([self getUserType]==3||[self getUserType]==1) {
                
                ChooseMore.ListParams=@{@"type":[NSString stringWithFormat:@"%ld",[LoginStatuesModel shoptype]],@"ptype":self.TPype};
                
            }else{
            
                ChooseMore.ListParams=@{@"ptype":self.TPype};
            }
            ChooseMore.OrinArr=list.items;
            
             ChooseMore.title=@"选择检查主题";
            
            ChooseMore.block=^(NSArray *ArmArr, NSInteger count)
            {
                 list.items=ArmArr;
                
                if (count==0) {
                    
                    list.value=@"";
                }else{
                
                list.value=[NSString stringWithFormat:@"已选%ld个主题",count];
                }
                [self.homeTableView reloadData];
            };
        
            [self.navigationController pushViewController:ChooseMore animated:YES];
            
        }else if ([list.fieldName isEqualToString:@"subjectId"]){
         
            NSDictionary *paraDict;
            
            if ([self getUserType]==3) {
                
                paraDict=@{@"type":[NSString stringWithFormat:@"%ld",[LoginStatuesModel shoptype]],@"ptype":@"2"};
                
            }else{
                
                paraDict=@{};
                
            }
            //自定义分类ID
            PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:GetSubjectList andArr:@[] andparam:paraDict];
            
            GroupView.block=^(NSString *empName, NSString *empID)
            {
                list.name=empName;
                
                list.value=empID;
                
                [self.homeTableView reloadData];
                
            };
            
            [GroupView PopWithView:kAppWindow];
            
            
        }else if ([list.fieldName isEqualToString:@"groupId"]){
            
            NSString *grouptype = self.isCheckGroup?@"2":@"1";
            
            //自定义分类ID
            PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:GetshopsGroup andArr:@[] andparam:@{@"staffId":[NSString stringWithFormat:@"%ld",[LoginStatuesModel getStaffId]],@"groupType":grouptype}];
            
            GroupView.block=^(NSString *empName, NSString *empID)
            {
                list.name=empName;
                
                list.value=empID;
         
                [self.homeTableView reloadData];
                
            };
            
            [GroupView PopWithView:kAppWindow];
            
            
        }else if ([list.fieldName isEqualToString:@"confirmState"]){
            
            UIActionSheet *sheet=[UIActionSheet bk_actionSheetWithTitle:@"确认状态"];
            
            [sheet bk_addButtonWithTitle:@"全部" handler:^{
                
                list.value=@"";
                
                list.name=@"全部";
                
                [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_addButtonWithTitle:@"未确认" handler:^{
                
                list.value=@"0";
                
                list.name=@"未确认";
                
                 [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_addButtonWithTitle:@"已确认" handler:^{
                
                  list.value=@"1";
                
                 list.name=@"已确认";
                
                [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_setCancelButtonWithTitle:@"取消" handler:^{
                
            }];
            
            [sheet showInView:self.view];
            
        }else if ([list.fieldName isEqualToString:@"rectifyState"]){
            
            UIActionSheet *sheet=[UIActionSheet bk_actionSheetWithTitle:@"整改状态"];
            
            [sheet bk_addButtonWithTitle:@"全部" handler:^{
                
                list.value=@"";
                
                list.name=@"全部";
                
                [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_addButtonWithTitle:@"未整改" handler:^{
                
                list.value=@"0";
                
                 list.name=@"未整改";
                
                 [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_addButtonWithTitle:@"已整改" handler:^{
                
                   list.value=@"1";
                
                  list.name=@"已整改";
                
                 [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_setCancelButtonWithTitle:@"取消" handler:^{
                
            }];
            
            [sheet showInView:self.view];
            
            
        }else if ([list.fieldName isEqualToString:@"shopSize"]){
            
            UIActionSheet *sheet=[UIActionSheet bk_actionSheetWithTitle:@"商户规模"];
            
            [sheet bk_addButtonWithTitle:@"全部" handler:^{
                
                list.value=@"";
                
                list.name=@"全部";
                
                [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_addButtonWithTitle:@"大型" handler:^{
                
                list.value=@"大型";
                
                 [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_addButtonWithTitle:@"中型" handler:^{
                
                list.value=@"中型";
                
                 [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_addButtonWithTitle:@"小型" handler:^{
                
                list.value=@"小型";
                
                 [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_addButtonWithTitle:@"微小型" handler:^{
                
                list.value=@"微小型";
                
                 [self.homeTableView reloadData];
                
            }];
            
            [sheet bk_setCancelButtonWithTitle:@"取消" handler:^{
                
            }];
            
            [sheet showInView:self.view];
            
            
        }else if ([list.fieldName isEqualToString:@"type"]){
            
            //餐饮流通
            
            PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:GetTypelist andArr:@[] andparam:@{}];
            
            GroupView.block=^(NSString *empName, NSString *empID)
            {
                list.name=empName;
                
                list.value=empID;
                
                [self.homeTableView reloadData];
                
            };
            
            [GroupView PopWithView:kAppWindow];
       
        }else if ([list.productType isEqualToString:@"商品分类"])
        {
            //分类
            PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:ProductType andArr:@[] andparam:@{}];
            
            GroupView.block=^(NSString *empName, NSString *empID)
            {
                list.name=empName;
                list.value=empID;
                
                [self.homeTableView reloadData];
                
            };
            
            [GroupView PopWithView:kAppWindow];
        }
        else if ([list.fieldName isEqualToString:@"catId"])
        {
            //添加剂分类
            
            PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:AdditiveType andArr:@[] andparam:@{}];
            
            GroupView.block=^(NSString *empName, NSString *empID)
            {
                list.name=empName;
                list.value=empID;
                
                [self.homeTableView reloadData];
                
            };
            
            [GroupView PopWithView:kAppWindow];
        }
        else if([list.fieldName isEqualToString:@"isAnswer"] || [list.fieldName isEqualToString:@"isMark"])
        {
            //是否交卷
        
            ChooseListData *list=self.CoventArr[indexPath.row];
            
            NSArray *nameArr = @[@"全部",@"否",@"是"];
            NSArray *valueArr = @[@"0",@"1",@"2"];
            
            NSMutableArray *listArr = [NSMutableArray array];
            for(int i =0;i<3;i++)
            {
                ChooseListItems *item = [ChooseListItems new];
                item.name = nameArr[i];
                item.value = valueArr[i];
                
                [listArr addObject:item];
            }
            list.items = [NSArray arrayWithArray:listArr];
            
            //躺框
            ChooseView *chooseView=[[ChooseView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kDevice_Is_iPhoneX?SCREEN_HEIGHT-64-IPhoneXTableHeight:SCREEN_HEIGHT-64) andDataArr:list.items andBlock:^(NSString *value, NSInteger index) {
                
                list.value=value;
                
                [self.homeTableView reloadData];
                
            }];
            
            [chooseView PopWithView:self.view];
        }
        else if ([list.fieldName isEqualToString:@"examId"])
        {
            //考题列表
            PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:ExaminationType andArr:@[] andparam:@{}];
            
            GroupView.block=^(NSString *empName, NSString *empID)
            {
                list.name=empName;
                list.value=empID;
                
                [self.homeTableView reloadData];
                
            };
            
            [GroupView PopWithView:kAppWindow];
        }
        else{
            
            //地区
            
            if ([list.fieldName isEqualToString:@"areaId1"])
            {
                
                PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:GetArealist andArr:@[] andparam:@{@"parentId":@"0"}];
                
                GroupView.block=^(NSString *empName, NSString *empID)
                {
                    list.name=empName;
                    
                    list.value=empID;
                    
                    //清空子项
                    for (ChooseListData *Sublist in self.CoventArr) {
                        
                        if ([Sublist.fieldName isEqualToString:@"areaId2"])
                        {
                            Sublist.name=@"";
                            
                            Sublist.value=@"";
                            
                        }else if ([Sublist.fieldName isEqualToString:@"areaId3"])
                        {
                            Sublist.name=@"";
                            
                            Sublist.value=@"";
                            
                        }else if ([Sublist.fieldName isEqualToString:@"areaId4"])
                        {
                            Sublist.name=@"";
                            
                            Sublist.value=@"";
                            
                        }
                        
                    }
                    
                    [self.homeTableView reloadData];
                    
                };
                
                [GroupView PopWithView:kAppWindow];
                
                
                
            }else if ([list.fieldName isEqualToString:@"areaId2"])
            {
                 ChooseListData *Flist=self.CoventArr[indexPath.row-1];
                
                if (Flist.value.length==0) {
                    
                    [MBProgressHUD showWarnMessage:@"请先选择省"];
                    
                    return;
                }
                
                PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:GetArealist andArr:@[] andparam:@{@"parentId":Flist.value}];
                
                GroupView.block=^(NSString *empName, NSString *empID)
                {
                    list.name=empName;
                    
                    list.value=empID;
                    
                    //清空子项
                    for (ChooseListData *Sublist in self.CoventArr) {
                        
                        if ([Sublist.fieldName isEqualToString:@"areaId3"])
                        {
                            Sublist.name=@"";
                            
                            Sublist.value=@"";
                            
                        }else if ([Sublist.fieldName isEqualToString:@"areaId4"])
                        {
                            Sublist.name=@"";
                            
                            Sublist.value=@"";
                            
                        }
                        
                    }
                    
                    [self.homeTableView reloadData];
                    
                };
                
                [GroupView PopWithView:kAppWindow];
                
                
            }else if ([list.fieldName isEqualToString:@"areaId3"])
            {
                
                ChooseListData *Flist=self.CoventArr[indexPath.row-1];
                
                if (Flist.value.length==0) {
                    
                    [MBProgressHUD showWarnMessage:@"请先选择市"];
                    
                    return;
                }
                
                PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:GetArealist andArr:@[] andparam:@{@"parentId":Flist.value}];
                
                GroupView.block=^(NSString *empName, NSString *empID)
                {
                    list.name=empName;
                    
                    list.value=empID;
                    
                    //清空子项
                    for (ChooseListData *Sublist in self.CoventArr) {
                        
                         if ([list.fieldName isEqualToString:@"areaId4"])
                        {
                            Sublist.name=@"";
                            
                            Sublist.value=@"";
                            
                        }
                        
                    }
                    
                    [self.homeTableView reloadData];
                    
                };
                
                [GroupView PopWithView:kAppWindow];
                
                
            }else{
                
                ChooseListData *Flist=self.CoventArr[indexPath.row-1];
                
                if (Flist.value.length==0) {
                    
                    [MBProgressHUD showWarnMessage:@"请先选择区"];
                    
                    return;
                }
                
                PositionSelectView *GroupView=[[PositionSelectView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, IPhoneXTableHeight?SCREEN_HEIGHT-IPhoneXTableHeight: SCREEN_HEIGHT) andType:GetArealist andArr:@[] andparam:@{@"parentId":Flist.value}];
                
                GroupView.block=^(NSString *empName, NSString *empID)
                {
                    list.name=empName;
                    
                    list.value=empID;
            
                    [self.homeTableView reloadData];
                    
                };
                
                [GroupView PopWithView:kAppWindow];
                
            }
    
        }
 
    }else{
        
    ChooseListData *list=self.SaveReqArr[indexPath.row-self.CoventArr.count];
    //躺框
    ChooseView *chooseView=[[ChooseView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kDevice_Is_iPhoneX?SCREEN_HEIGHT-64-IPhoneXTableHeight:SCREEN_HEIGHT-64) andDataArr:list.items andBlock:^(NSString *value, NSInteger index) {
        
        list.value=value;
        
        [self.homeTableView reloadData];
        
    }];
    
        [chooseView PopWithView:self.view];
        
        }
    }
}


-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    
    view.tintColor=[UIColor groupTableViewBackgroundColor];
}

- (void)actionTapEmptyDataSetNodataView
{
     [self loadDatas];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
